package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.entity.regi.CqFailureModesEntity;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.excel.annotation.ResponseExcel;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.regi.service.CqFailureModesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 失效模式
 *
 * @author lucc
 * @date 2023-10-18 11:36:49
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqFailureModes")
@Tag(description = "cqFailureModes", name = "失效模式管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqFailureModesController {
    private final CqFailureModesService cqFailureModesService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param cqFailureModes 失效模式
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    // @PreAuthorize("@pms.hasPermission('eam_cqFailureModes_view')" )
    public R getCqFailureModesPage(@ParameterObject Page page, @ParameterObject CqFailureModesEntity cqFailureModes) {
        LambdaQueryWrapper<CqFailureModesEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(cqFailureModes.getDescription()), CqFailureModesEntity::getDescription, cqFailureModes.getDescription());
        wrapper.eq(Objects.nonNull(cqFailureModes.getCategory()), CqFailureModesEntity::getCategory, cqFailureModes.getCategory());
        wrapper.eq(Objects.nonNull(cqFailureModes.getImpact()), CqFailureModesEntity::getImpact, cqFailureModes.getImpact())
                .orderByDesc(CqFailureModesEntity::getCreateTime);

        return R.ok(cqFailureModesService.page(page, wrapper));
    }


    /**
     * 通过id查询失效模式
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(cqFailureModesService.getById(id));
    }

    /**
     * 新增失效模式
     *
     * @param cqFailureModes 失效模式
     * @return R
     */
    @Operation(summary = "新增失效模式", description = "新增失效模式")
    @SysLog("新增失效模式")
    @PostMapping
    public R save(@RequestBody CqFailureModesEntity cqFailureModes) {
        return R.ok(cqFailureModesService.save(cqFailureModes));
    }

    /**
     * 修改失效模式
     *
     * @param cqFailureModes 失效模式
     * @return R
     */
    @Operation(summary = "修改失效模式", description = "修改失效模式")
    @SysLog("修改失效模式")
    @PutMapping
    public R updateById(@RequestBody CqFailureModesEntity cqFailureModes) {
        return R.ok(cqFailureModesService.updateById(cqFailureModes));
    }

    /**
     * 通过id删除失效模式
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除失效模式", description = "通过id删除失效模式")
    @SysLog("通过id删除失效模式")
    @DeleteMapping
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(cqFailureModesService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param cqFailureModes 查询条件
     * @param ids            导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<CqFailureModesEntity> export(CqFailureModesEntity cqFailureModes, Integer[] ids) {
        return cqFailureModesService.list(Wrappers.lambdaQuery(cqFailureModes).in(ArrayUtil.isNotEmpty(ids), CqFailureModesEntity::getId, ids));
    }
}
